package com.cpecc.controller.wuzi;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.manager.system.WuZiLBManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.model.wuzi.WuZi;

@Controller
@RequestMapping(value = "/wuzi")
public class GuanLianController {
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private WuZiLBManager wuZiLBManager;
	
	@RequestMapping(value = "/guanLianPJ")
	public ModelAndView guanLianPJ(String pid){
		ModelAndView mav=new ModelAndView();
		mav.addObject("pid", pid);
		mav.addObject("wuZi", wuZiManager.get(pid));
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
		mav.setViewName("wuzi/guanLianPJ");
		return mav;
	}
	
	@RequestMapping(value = "/guanLianChaXunPJ")
	public ModelAndView guanLianChaXunPJ(WuZi wuZi,String wuZiLBId){
	
		ModelAndView mav=new ModelAndView();		
		mav.addObject("guanLianPJList", wuZiManager.getWuZiList(wuZi, wuZiLBId, 0,"",""));
		mav.setViewName("wuzi/guanLianPJListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/guanLianPJXZ")
	public void guanLianPJXZ(String pid,String id,HttpServletResponse response){		
		WuZi parent=wuZiManager.get(pid);
		WuZi child=wuZiManager.get(id);
		child.setParent(parent);
		wuZiManager.cover(child);
		PrintWriter out = null;
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/shanChuGLPJ")
	public void shanChuGLPJ(String cid,HttpServletResponse response){
		WuZi child=wuZiManager.get(cid);
		child.setParent(null);
		wuZiManager.cover(child);
		PrintWriter out = null;
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/guanLianZJ")
	public ModelAndView guanLianZJ(String cid){
		ModelAndView mav=new ModelAndView();
		mav.addObject("cid", cid);
		mav.addObject("wuZi", wuZiManager.get(cid));
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
		mav.setViewName("wuzi/guanLianZJ");
		return mav;
	}
	
	@RequestMapping(value = "/guanLianChaXunZJ")
	public ModelAndView guanLianChaXunZJ(WuZi wuZi,String wuZiLBId){
		ModelAndView mav=new ModelAndView();
		mav.addObject("guanLianPJList", wuZiManager.getWuZiList(wuZi,wuZiLBId,0,"",""));
		mav.setViewName("wuzi/guanLianZJListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/guanLianZJXZ")
	public void guanLianZJXZ(String cid,String id,HttpServletResponse response){
		WuZi parent=wuZiManager.get(id);
		WuZi child=wuZiManager.get(cid);
		child.setParent(parent);
		wuZiManager.cover(child);
		PrintWriter out = null;
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/shanChuGLZJ")
	public void shanChuGLZJ(String cid,HttpServletResponse response){
		WuZi child=wuZiManager.get(cid);
		child.setParent(null);
		wuZiManager.cover(child);
		PrintWriter out = null;
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}
	}
	
}
